import type { Reducer, Effect } from 'umi';
import { getStaffInfoSource, clickrate } from '../services/user';
import { requestCode } from '@/utils/constans';

export type menuNavBarState = {}[];

export type StaffInfoSourceState = {
  id?: string;
  tenantId?: string;
  tenantCode?: string;
  resourceName?: string;
  resourceCode?: string;
  resourceType?: string;
  resourceClass?: string;
  pid?: string | null;
  appId?: string | null;
  appName?: string;
  appCode?: string | null;
  menuRul?: string | null;
  compPath?: string | null;
  openType?: string;
  startDate?: string;
  endDate?: string;
  classCode?: string;
  resSort?: string | null;
  status?: string;
  createStaffId?: string;
  createTime?: string;
  updateStaffId?: string;
  updateTime?: string;
  orgCode?: string;
  regionId: string | null;
  regionCode: string | null;
  positionId: string | null;
  positionCode: string | null;
  children: StaffInfoSourceState[];
  path?: string;
  name?: string;
}[];

export type MenuModelState = {
  staffInfoSource: StaffInfoSourceState;
  menuNavBar: menuNavBarState;
  // 省分tab数据
  provinceMenuBar: [];
  // 系统中心tab数据
  systemMenuBar: [];
  // 权力清单tab数据
  powfeMenuBar: [];
  // 集团权力清单tab数据
  groupPowfeMenuBar: [];
};

export type MenuModelType = {
  namespace: 'menuslider';
  state: MenuModelState;
  effects: {
    addMenuNavBar: Effect;
    deleteMenuNavBar: Effect;
    staffInfoSourceFetch: Effect;
    clickrate: Effect;
  };
  reducers: {
    saveMenuNavBar: Reducer<MenuModelState>;
    removeMenuNavBar: Reducer<MenuModelState>;
    saveStaffInfoResource: Reducer<MenuModelState>;
    saveClickrate: Reducer<MenuModelState>;
    saveProvinceMenuBar: Reducer<MenuModelState>;
    saveSystemMenuBar: Reducer<MenuModelState>;
    savePowfeMenuBar: Reducer<MenuModelState>;
    saveGroupPowfeMenuBar: Reducer<MenuModelState>;
  };
};

const MenuModel: MenuModelType = {
  namespace: 'menuslider',

  state: {
    staffInfoSource: [],
    menuNavBar: [{ path: '/list', name: '主页' }],
    provinceMenuBar: [],
  },

  effects: {
    *staffInfoSourceFetch({ callback, payload }, { call, put }) {
      const response = yield call(getStaffInfoSource, payload);
      const { data, status } = response;
      // console.log('staffInfoSourceFetch--->', data, status)
      if (status === '00000') {
        // message.success(`🎉 🎉 🎉  ${requestCode[status]}！`);
        yield put({
          type: 'saveStaffInfoResource',
          payload: data,
        });
        if (callback) {
          callback(data);
        }
        return Promise.resolve(data);
      }
      return Promise.reject(requestCode[status]);
    },
    *addMenuNavBar({ payload }, { put }) {
      yield put({
        type: 'saveMenuNavBar',
        payload,
      });
    },

    *deleteMenuNavBar({ payload }, { put }) {
      // console.log('changeMenuList:payload--->', payload)
      yield put({
        type: 'saveMenuNavBar',
        payload,
      });
    },
    *clickrate({ payload, callback }, { call, put }) {
      const response = yield call(clickrate, payload);
      yield put({
        type: 'saveClickrate',
        payload: response,
      });
      if (response) callback(response);
    },
  },

  reducers: {
    saveStaffInfoResource(state, { payload }) {
      return {
        ...state,
        staffInfoSource: payload || {},
      };
    },
    saveMenuNavBar(state: any, { payload }: any) {
      return {
        ...state,
        menuNavBar: payload || [],
      };
    },
    removeMenuNavBar(state: any, { payload }: any) {
      return {
        ...state,
        menuNavBar: payload || [],
      };
    },
    saveClickrate(state: any, action: any) {
      return {
        ...state,
        type: action.type,
      };
    },
    saveProvinceMenuBar(state: any, { payload }: any) {
      return {
        ...state,
        provinceMenuBar: payload,
      };
    },
    saveSystemMenuBar(state: any, { payload }: any) {
      return {
        ...state,
        systemMenuBar: payload,
      };
    },
    savePowfeMenuBar(state: any, { payload }: any) {
      return {
        ...state,
        powfeMenuBar: payload,
      };
    },
    saveGroupPowfeMenuBar(state: any, { payload }: any) {
      return {
        ...state,
        groupPowfeMenuBar: payload,
      };
    },
  },
};

export default MenuModel;
